<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 16/3/29
 * Time: 上午10:53
 */
class OdsCarController extends BaseController{
    /*
     * 查询车辆型号
     * */
    public function models(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'brand' => 'required',
                'series' => 'required',
                'year' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $brand = Input::get('brand');
            $series = Input::get('series');
            $year = Input::get('year');

            //验证token
            $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
            if(is_numeric($car_data) && $car_data < 0){
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            //获取品牌车辆的厂商
            $models = CarStandardInfo::where('brand_name_id',$brand)
                ->where('series_name_id',$series)
                ->where('model_year',$year)
                ->select(array('id','model_name as model'))
                ->distinct()
                ->get()->toArray();
            $count = count($models);
            $result = array(
                'count' => $count,
                'models' => $models
            );

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "models", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 查询这辆年款
     * */
    public function years(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'brand' => 'required',
                'series' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $brand = Input::get('brand');
            $series = Input::get('series');

            //验证token
            $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
            if(is_numeric($car_data) && $car_data < 0){
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            //获取品牌车辆的厂商
            $years = CarStandardInfo::where('brand_name_id',$brand)
                ->where('series_name_id',$series)
                ->select(array('model_year as year'))
                ->distinct()
                ->get()->toArray();
            $count = count($years);
            $result = array(
                'count' => $count,
                'years' => $years
            );

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "years", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 查询车辆系列
     * */
    public function series(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'brand' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $brand = Input::get('brand');

            //验证token
            $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
            if(is_numeric($car_data) && $car_data < 0){
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            //获取品牌车辆的厂商
            $factories = CarStandardInfo::where('brand_name_id',$brand)
                ->select(array('series_shop_id','series_shop'))
                ->distinct()
                ->get();
            $car_factories = array();
            if(!is_null($factories)){
                foreach ($factories as $_factory) {
                    $series = CarStandardInfo::where('brand_name_id',$brand)
                        ->where('series_shop_id',$_factory->series_shop_id)
                        ->select(array('series_name_id as id','series_name as name'))
                        ->distinct()
                        ->get()->toArray();
                    $factory = array(
                        'factory' => $_factory->series_shop,
                        'count' => count($series),
                        'series' => $series
                    );
                    array_push($car_factories,$factory);
                }
            }
            $result = array(
                'count' => count($car_factories),
                'factories' => $car_factories
            );
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "series", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 查询车辆品牌
     * */
    public function brands(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');

            //验证token
            $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
            if(is_numeric($car_data) && $car_data < 0){
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            //获取车辆品牌信息
            $brands = CarBrandType::select(array('id','initial','name','icon'))->get()->toArray();
            $count = count($brands);
            $result = array(
                'count' => $count,
                'base_icon_url' => Config::get('car.base_icon_url'),
                'brands' => $brands
            );

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "brands", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}